package com.wooboo.server.ext.action.traffic;


import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.data.SysLogger;
import com.wooboo.server.ext.data.Traffic;
import com.wooboo.server.ext.data.UserFinance;
import com.wooboo.server.ext.service.DbService;
import com.wooboo.server.ext.util.DateUtil;
import com.wooboo.server.ext.util.HttpClientUtils;
import com.wooboo.server.ext.util.NumberUtil;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;
import com.wooboo.server.ext.util.http.HttpUtils;

import net.sf.json.JSONObject;

/**
 * @author www.wooboo.com.cn
 * 流量汇企业信息注册申请
 */
public class Register2 extends ActionBase{
	public Integer advertiserId;//广告主
	public String company_name;//公司名称
	public String city_name;//城市名称
	public String contact_mobile;//电话
	public String contact_email;//邮件
	
	public Register2(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		company_name = params.getFieldValue("company_name");
		advertiserId = NumberUtil.getInteger(params.getFieldValue("advertiserId"));
		if(StringUtil.isEmpty(advertiserId)){
		    throw ApiError.Type.INVALID_PARAM.toException();
		}
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		DbService dbService = new DbService();
		UserFinance userfinance=dbService.findUserFinanceById(advertiserId);
		if(null==userfinance){
			throw ApiError.Type.INVALID_PARAM.toException("账户信息不存在。");
		}
		String dateStr = DateUtil.getFormatDate(new Date(), "yyyyMMddHHmmss");
		SortedMap<String, String> params =  new TreeMap<String, String>();
		params.put("portalType", Traffic.PORTAL_TYPE);//门户类型 
		params.put("portalID", Traffic.PORTAL_ID);
		params.put("transactionID",dateStr+StringUtil.createId(8));//交易唯一编码：yyyMMddHHmmss+8位唯一序号
		params.put("method", Traffic.Method.companyRegisterApply.toString());//方法
		params.put("company_name",company_name);//公司
		params.put("create_time", dateStr);//时间戳
		params.put("province_name", "beijing");
		params.put("company_addr", "beijing");
		params.put("company_nature", "0");
	//	params.put("contact_mobile", StringUtil.dealNull(contact_mobile));
		params.put("contact_email", "fujianyd@163.com");
		params.put("sign", Traffic.sign(params));
		//params.put("contact_email", StringUtil.encode(params.get("contact_email"), "utf-8"));
		//System.out.println(params.get("signs"));

		
		//JSONObject response= HttpUtils.postFromUrl4Json(Traffic.TRAFFIC_CALL_URI, params);
		
		JSONObject response= HttpClientUtils.postFromUrl4Json(Traffic.TRAFFIC_CALL_URI, params);


		if(null==response){
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇注册接口失败！");
		}
		String status = response.optString("status");
		String code=response.optString("code");
		if("ok".equalsIgnoreCase(status) && "0".equals(code)){
			String company_code = response.optJSONObject("result").getString("company_code");
		}
		
		
//		
//		SysLogger logger = new SysLogger();
//		logger.business_code="traffic";
//		logger.business_id=userfinance.uid;
//		logger.create_user=user.user_id;
//		logger.operate_name="流量汇注册";
//		logger.create_time=new Date();
//		logger.user_name=user.user_name;
//		
//		StringBuffer sbuf = new StringBuffer();
//		if(null==response){
//			sbuf.append("调用流量汇注册接口失败！返回response为null");
//			logger.operate_desc=sbuf.toString();
//			//dbService.saveLogger(logger);
//			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇注册接口失败！");
//		}
//		
//		String status = response.optString("status");
//		String code=response.optString("code");
//		sbuf.append("[status="+status+"]");
//		if("ok".equalsIgnoreCase(status) && "0".equals(code)){
//			//注册成功
//			
//			String company_code = response.optJSONObject("result").getString("company_code");
//			sbuf.append("流量汇用户注册成功，【company_code="+company_code+"】");
//			userfinance.setTraffic_company_code(company_code);
//			dbService.update4Register(userfinance);
//			//dbService.saveLogger(logger);
//		}else{
//			sbuf.append("流量汇用户注册失败，【status="+status+",code="+code+"】");
//			//dbService.saveLogger(logger);
//		}
	    return result;
	}
	
}
